﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace TM.Model.Mapper
{
    public class ProjectTaskMapper : IMapper<ProjectTask>
    {
        #region Implementation of IMapper<ProjectTask>

        public ProjectTask Map(DataRow dr)
        {
            return new ProjectTask()
                       {
                           ID = new Guid(dr["ID"].ToString()),
                           Name = dr["Name"].ToString(),
                           ParentID = (dr["ParentID"] is System.DBNull || string.IsNullOrEmpty(dr["ParentID"].ToString()) ? new Guid?() : new Guid(dr["ParentID"].ToString())),
                           ProjectID = new Guid(dr["ProjectID"].ToString()),
                           DescDetails = (dr["DescDetails"] is System.DBNull ? string.Empty : dr["DescDetails"].ToString()),
                           TypeID = new Guid(dr["TypeID"].ToString()),
                           StatusID = new Guid(dr["StatusID"].ToString()),
                           PriorityID = new Guid(dr["PriorityID"].ToString()),
                           OwnerID = new Guid(dr["OwnerID"].ToString()),
                           RawEstimation = (dr["RawEstimation"] is System.DBNull ? new decimal?() : decimal.Parse(dr["RawEstimation"].ToString())),
                           ActualEstimation = (dr["ActualEstimation"] is System.DBNull ? new decimal?() : decimal.Parse(dr["ActualEstimation"].ToString())),
                           StartDate = (dr["StartDate"] is System.DBNull ? new DateTime?() : DateTime.Parse(dr["StartDate"].ToString())),
                           EndDate = (dr["EndDate"] is System.DBNull ? new DateTime?() : DateTime.Parse(dr["EndDate"].ToString())),
                           SpentTime = (dr["SpentTime"] is System.DBNull ? new decimal?() : decimal.Parse(dr["SpentTime"].ToString())),
                           TimeToComplete = (dr["TimeToComplete"] is System.DBNull ? new decimal?() : decimal.Parse(dr["TimeToComplete"].ToString())),
                           Resources = (dr["Resources"] is System.DBNull ? string.Empty : dr["Resources"].ToString()),
                           Scopes = (dr["Scopes"] is System.DBNull ? string.Empty : dr["Scopes"].ToString()),
                           Evidents = (dr["Evidents"] is System.DBNull ? string.Empty : dr["Evidents"].ToString()),
                           Methology = (dr["Methology"] is System.DBNull ? string.Empty : dr["Methology"].ToString()),
                           CreatedDate = DateTime.Parse(dr["CreatedDate"].ToString())
                       };
        }

        public IList<ProjectTask> Map(DataTable tb)
        {
            if (tb == null || tb.Rows.Count == 0) return new List<ProjectTask>();

            IList<ProjectTask> lst = new List<ProjectTask>();
            foreach (DataRow row in tb.Rows)
            {
                lst.Add(Map(row));
            }

            return lst;
        }

        #endregion
    }
}
